# Add modules
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})

############################################################
# MACRO add_library_for_config
#
# Add a library for a certain configuration, e.g. 'debug' or 'optimized'.
# The macro checks if $input is set, if yes, it adds it to the output variable
# with the given prefix. If $input is not set, but $fallback is set, $fallback is
# used instead, e.g. to allow for debug libraries to be used if the release version is
# not available or vice versa.
#
# example call: add_library_for_config(MY_LIBRARY optimized MY_LIBRARY_RELEASE MY_LIBRARY_DEBUG)
#
macro(add_library_for_config dest prefix input fallback)
  if(${input})
    set(${dest} ${${dest}} ${prefix} ${${input}})
  elseif(${fallback})
    set(${dest} ${${dest}} ${prefix} ${${fallback}})
  endif()
endmacro(add_library_for_config dest prefix input fallback)

macro(qt4_auto_wrap outfiles)
    # clear accumulating variables
    unset(moc_headers)
    unset(ui_files)
    unset(qrc_files)

    # generate list of moc, uic, qrc files
    foreach(fileName ${ARGN})
        # moc: headers (WITH .h) need to contain "Q_OBJECT"
        if(fileName MATCHES "\\.h$")
            file(STRINGS ${fileName} lines REGEX Q_OBJECT)
            if(lines)
                set(moc_headers ${moc_headers} ${fileName})
                message(STATUS "moc: ${fileName}")
            endif()
        endif()

        # uic: files have extension ".ui"
        if(fileName MATCHES "\\.ui$")
            set(ui_files ${ui_files} ${fileName})
            message(STATUS "uic: ${fileName}")
        endif()

        # qrc: files have extension ".qrc"
        if(fileName MATCHES "\\.qrc$")
            set(qrc_files ${qrc_files} ${fileName})
            message(STATUS "qrc: ${fileName}")
        endif()
    endforeach()

    # use standard functions to handle these files
    QT4_WRAP_CPP(${outfiles} ${moc_headers})
    QT4_WRAP_UI(${outfiles} ${ui_files})
    QT4_ADD_RESOURCES(${outfiles} ${qrc_files})

    # add include directory for generated ui_*.h files
    include_directories(${CMAKE_CURRENT_BINARY_DIR})
endmacro(qt4_auto_wrap)
